import networkx as nx

# 读取数据集
G = nx.read_edgelist('email-Network.txt', create_using=nx.DiGraph())

# 将节点的部门属性添加到有向图中
department_labels = {}
with open('email-Department.txt', 'r') as file:
    for line in file:
        #split()默认以空格的方式分割
        node, label = line.strip().split()
        department_labels[node] = label

nx.set_node_attributes(G, department_labels, 'department')

# 有向图桥接性指数计算
def directed_bridge_connectivity_index(graph, edge):
    global G
    # 创建临时图，移除指定的边
    temp_graph = graph.copy()
    temp_graph.remove_edge(*edge)

    # 计算原始图和临时图的强连通分量数量
    scc_original = nx.number_strongly_connected_components(graph)
    scc_temp = nx.number_strongly_connected_components(temp_graph)

    # 计算有向桥接性指数
    directed_bridge_index = scc_temp - scc_original

    return directed_bridge_index


for edge in G.edges:
    bridge_index=directed_bridge_connectivity_index(G, edge)
    print(f"{edge[0]},{edge[1]},{bridge_index}")